{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import crop\n",
    "import numpy as np\n",
    "%matplotlib notebook\n",
    "from  matplotlib import pyplot as plt\n",
    "import cv2\n",
    "input_path = r\"C:\\Users\\gulugulu1103\\OneDrive\\Python\\scanned-image-processing\\input\\\\\"\n",
    "input_filename = input_path + \"example2.jpg\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-31T17:18:03.490562600Z",
     "start_time": "2023-05-31T17:18:03.018930Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "img = cv2.imread(input_filename, 1)\n",
    "# display image in jupyter notebook\n",
    "plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-31T17:18:03.963771100Z",
     "start_time": "2023-05-31T17:18:03.954769Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "首先，找到最大的矩形，并且显示出来"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "times = 1\n",
      "Detect with threshold: 210\n",
      "Found an image !\n",
      "Contours 6 with 1 large and 1 images found\n",
      "large_contours: 1 & len(kept_contours) = 1\n"
     ]
    }
   ],
   "source": [
    "length, found = crop.cont(\n",
    "        img = img,\n",
    "        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),\n",
    "        user_thresh = 210,\n",
    "        crop = False)\n",
    "# combine all the img in found to display\n",
    "plt.imshow(cv2.cvtColor(found[-1], cv2.COLOR_BGR2RGB))\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-31T17:18:14.223452800Z",
     "start_time": "2023-05-31T17:18:14.191574700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "times = 1\n",
      "Detect with threshold: 210\n",
      "Found an image !\n",
      "Contours 5 with 1 large and 1 images found\n",
      "large_contours: 1 & len(kept_contours) = 1\n"
     ]
    }
   ],
   "source": [
    "length, found = crop.cont(\n",
    "        img = img,\n",
    "        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),\n",
    "        user_thresh = 210,\n",
    "        crop = False,\n",
    "        show_rect = False)\n",
    "plt.imshow(cv2.cvtColor(found[-1], cv2.COLOR_BGR2RGB))\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-31T17:18:18.332303300Z",
     "start_time": "2023-05-31T17:18:18.304379100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
